package com.jzg.sys.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzg.sys.api.sec.UserService;
import com.jzg.sys.dao.entity.SecRole;
import com.jzg.sys.dao.entity.SecResource;
import com.jzg.sys.dao.entity.SecRole;
import com.jzg.sys.dao.entity.SecUser;
import com.jzg.sys.dao.mapper.SecUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import com.jzg.sys.dao.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
    * 用户 服务实现类
    * </p>
 *
 * @author lzp
 * @since 2019-02-21
 */
@Service
public class SecUserManager extends ServiceImpl<SecUserMapper, SecUser>  implements UserService {
    @Resource
    private SecUserMapper secUserMapper;
    @Resource
    private SecUserRoleMapper userRoleMapper;
    @Resource
    private SecRoleResourceMapper roleResourceMapper;
    @Resource
    private SecResourceMapper resourceMapper;
    @Resource
    private SecRoleMapper roleMapper;

    public SecUser findUserByCode(String userCode, String sysCode) {
        return secUserMapper.selectOne(new QueryWrapper<SecUser>()
                .lambda()
                .eq(SecUser::getUserCode, userCode)
                .eq(SecUser::getSysCode, sysCode));
    }

    public IPage<SecUser> findEasyPage(Page<SecUser> page, SecUser secUser) {
        return secUserMapper.selectPage(page,new QueryWrapper<SecUser>()
                .lambda()
                .eq(StringUtils.isNotBlank(secUser.getSysCode()),SecUser::getSysCode,secUser.getSysCode())
                .like(StringUtils.isNotBlank(secUser.getUserCode()),SecUser::getUserCode,secUser.getUserCode())
                .like(StringUtils.isNotBlank(secUser.getUserName()),SecUser::getUserName,secUser.getUserName())
                .like(StringUtils.isNotBlank(secUser.getPhone()),SecUser::getPhone,secUser.getPhone()));
    }

    @Override
    public SecUser findUserById(String userId) {
        return secUserMapper.selectById(userId);
    }

    @Override
    public List<String> getResourcesCode(String userId) {
        if (userId.equals("1")) {
            List<SecResource> list = resourceMapper.selectList(new QueryWrapper<SecResource>()
                    .select("resource_code as resourceCode"));
            if (!list.isEmpty()) {
                return list.stream()
                        .map(SecResource::getResourceCode)
                        .collect(Collectors.toList());
            }
        }else {
           return resourceMapper.selectResourcesCodeByUserId(userId);
        }
        return new ArrayList<>();
    }

    @Override
    public List<String> getRolesCode(String userId) {
        if (userId.equals("1")) {
            List<SecRole> list = roleMapper.selectList(new QueryWrapper<SecRole>()
                    .select("role_code as roleCode"));
            if (!list.isEmpty()) {
                return list.stream()
                        .map(SecRole::getRoleCode)
                        .collect(Collectors.toList());
            }
        }else {
            return roleMapper.selectRoleCodeByUserId(userId);
        }
        return new ArrayList<>();
    }

}
